//
//  DCRobCell.m
//  Driver
//
//  Created by 大橙子 on 2018/11/8.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "DCRobCell.h"
@interface DCRobCell ()
@property (nonatomic,strong) UIView *firstView;
@property (nonatomic,strong) UIImageView *carrierImage;
@property (nonatomic,strong) UILabel *carrierLabel;//承运商
@property (nonatomic,strong) UILabel *timeLabel;
@property (nonatomic,strong) UILabel *stateLabel;

@property (nonatomic,strong) UIView *secondView;
@property (nonatomic,strong) DCImageAndTextLabel *startLabel;
@property (nonatomic,strong) UILabel *invoiceLabel;
@property (nonatomic,strong) DCImageAndTextLabel *endLabel;
@property (nonatomic,strong) DCImageAndTextLabel *priceLabel;
@property (nonatomic,strong) DCImageAndTextLabel *carLabel;
@property (nonatomic,strong) UILabel *totalNumLabel;

@property (nonatomic,strong) UIView *thirdView;
@property (nonatomic,strong) UIButton *tapBtn;
@end
@implementation DCRobCell
+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *CellID = @"Cell";
    DCRobCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil) {
        cell = [[DCRobCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = BackgroundColor;
    }
    return cell;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpUI];
    }
    return self;
}
-(void)setUpUI
{
    UIView *firstView = [[UIView alloc]init];
    firstView.backgroundColor = [UIColor whiteColor];
    self.firstView = firstView;
    [self addSubview:firstView];
    
    UIImageView *carrierImage = [[UIImageView alloc]initWithImage:IMAGENAME(@"用户头像")];
    //    carrierImage.backgroundColor = [UIColor redColor];
    self.carrierImage = carrierImage;
    [self.firstView addSubview:carrierImage];
    
    self.carrierLabel = [self getLabelWithFont:17 textAlignment:NSTextAlignmentLeft];
    [self.firstView addSubview:self.carrierLabel];
    
    self.timeLabel = [self getLabelWithFont:14 textAlignment:NSTextAlignmentCenter];
    self.timeLabel.textColor = [UIColor lightGrayColor];
    [self.firstView addSubview:self.timeLabel];
    
    self.stateLabel = [self getLabelWithFont:15 textAlignment:NSTextAlignmentRight];
    //    self.stateLabel.backgroundColor = BlueColor;
    [self.firstView addSubview:self.stateLabel];
    
    UIView *secondView = [[UIView alloc]init];
    secondView.backgroundColor = [UIColor whiteColor];
    self.secondView = secondView;
    [self addSubview:secondView];
    
    DCImageAndTextLabel *startLabel = [[DCImageAndTextLabel alloc]init];
    startLabel.imageView.image = IMAGENAME(@"起点");
    self.startLabel = startLabel;
    [self.secondView addSubview:startLabel];
    
    self.invoiceLabel = [[UILabel alloc]init];
    [self.secondView addSubview:self.invoiceLabel];
    
    DCImageAndTextLabel *endLabel = [[DCImageAndTextLabel alloc]init];
    endLabel.imageView.image = IMAGENAME(@"终点");
    self.endLabel = endLabel;
    [self.secondView addSubview:endLabel];
    
    self.priceLabel = [[DCImageAndTextLabel alloc]init];
    self.priceLabel.imageView.image = IMAGENAME(@"钱盾");
    self.priceLabel.textLabel.textColor = BlueColor;
    [self.secondView addSubview:self.priceLabel];
    
    self.carLabel = [[DCImageAndTextLabel alloc]init];
    self.carLabel.imageView.image = IMAGENAME(@"立方体");
    self.carLabel.textLabel.font = FONT(14);
    //    self.carLabel.backgroundColor = [UIColor redColor];
    [self.secondView addSubview:self.carLabel];
    
    self.totalNumLabel = [self getLabelWithFont:15 textAlignment:NSTextAlignmentRight];
    //    self.totalNumLabel.backgroundColor = [UIColor yellowColor];
    [self.secondView addSubview:self.totalNumLabel];
    
    self.thirdView = [[UIView alloc]init];
    self.thirdView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.thirdView];
    
    UIButton *tapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [tapBtn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [tapBtn setTitle:@"抢单" forState:UIControlStateNormal];
    [tapBtn setTitleColor:BlueColor forState:UIControlStateNormal];
    tapBtn.titleLabel.font = FONT(15);
    tapBtn.layer.cornerRadius = 15;
    tapBtn.clipsToBounds = YES;
    tapBtn.layer.borderColor = BlueColor.CGColor;
    tapBtn.layer.borderWidth = 1;
    self.tapBtn = tapBtn;
    [self.thirdView addSubview:tapBtn];
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat kMargin = 10;
    //    CGFloat kHeight = 40;
    self.firstView.frame = CGRectMake(0, kMargin, self.width, kBaseHeight);
    
    self.carrierImage.frame = CGRectMake(10, 3, self.firstView.height - 6, self.firstView.height - 6);
    self.carrierImage.layer.cornerRadius = self.carrierImage.width/2;
    
    self.carrierLabel.frame = CGRectMake(self.carrierImage.right+3, 0, 70*WIDTHSCALE6, self.firstView.height);
    
    self.timeLabel.width = 150*WIDTHSCALE6;
    self.timeLabel.height = self.firstView.height;
    self.timeLabel.y = 0;
    self.timeLabel.centerX = self.firstView.centerX;
    
    __weak typeof(self) weakself = self;
    [self.stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.firstView);
        make.right.equalTo(weakself.firstView.mas_right).offset(-3);
        make.left.equalTo(weakself.timeLabel.mas_right).offset(3);
        make.height.mas_offset(weakself.firstView.height);
    }];
    
    self.secondView.frame = CGRectMake(0, self.firstView.bottom+1, self.firstView.width, (kBaseHeight-10)*3);
    
    self.startLabel.frame = CGRectMake(self.secondView.width/4, 0, self.secondView.width*3/8, kBaseHeight-10);
    self.invoiceLabel.frame = CGRectMake(self.startLabel.right, 0, self.startLabel.width, self.startLabel.height);
    self.endLabel.frame = CGRectMake(self.startLabel.x, self.startLabel.bottom, self.startLabel.width, self.startLabel.height);
    
    self.priceLabel.frame = CGRectMake(self.invoiceLabel.x, self.endLabel.y, self.invoiceLabel.width, self.startLabel.height);
    
    self.carLabel.frame = CGRectMake(self.secondView.width/4, self.endLabel.bottom, 80*WIDTHSCALE6, self.startLabel.height);
    self.totalNumLabel.frame = CGRectMake(self.carLabel.right+30, self.carLabel.y, 60*WIDTHSCALE6, self.carLabel.height);
    
    if ([self.model.type isEqualToString:@"待抢单"]) {
        self.invoiceLabel.hidden = YES;
//        self.priceLabel.hidden = YES;
        
        self.thirdView.frame = CGRectMake(0, self.secondView.bottom+1, self.width, kBaseHeight);
        self.tapBtn.frame = CGRectMake(self.thirdView.width - 90*WIDTHSCALE6, 5, 80*WIDTHSCALE6, self.thirdView.height-10);
    }
    //    cellHeight = CGRectGetHeight(self.firstView.frame)+CGRectGetHeight(self.secondView.frame)+CGRectGetHeight(self.thirdView.frame)+10;
}
-(void)setModel:(DCRobModel *)model
{
    _model = model;
    self.carrierLabel.text = model.carrier;
    self.timeLabel.text = model.time;
    self.stateLabel.text = model.type;
    self.stateLabel.textColor = [UIColor redColor];
    
    self.startLabel.textLabel.text = model.start.length>6?[model.start substringToIndex:6]:model.start;
    if ([model.invoice isEqualToString:@"开票"]) {
//        self.invoiceLabel.textColor = DCColor(133, 195, 96);
        self.invoiceLabel.textColor = BlueColor;
    }else{
        self.invoiceLabel.textColor = BlueColor;
    }
    self.invoiceLabel.text = model.invoice;
    
    self.endLabel.textLabel.text = model.end.length>6?[model.end substringToIndex:6]:model.end;
    self.priceLabel.textLabel.text = model.price;
    
    self.carLabel.textLabel.text = model.car;
    self.totalNumLabel.text = [NSString stringWithFormat:@"%@辆",model.num];
    
    //    cellHeight = CGRectGetHeight(self.firstView.frame)+CGRectGetHeight(self.secondView.frame)+CGRectGetHeight(self.thirdView.frame)+10;
}
-(void)btnDidClick:(UIButton *)button
{
    DCLog(@"待成交");
    if (_delegate && [_delegate respondsToSelector:@selector(selectedWithOfferBtn:)]) {
        [_delegate selectedWithOfferBtn:self.model];
    }
    
}
-(UILabel *)getLabelWithFont:(NSInteger)font textAlignment:(NSTextAlignment)textAlignment
{
    UILabel *label = [[UILabel alloc]init];
    label.font = FONT(font);
    label.textAlignment = textAlignment;
    return label;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
